package org.feiye.studyday28.case3;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

//登陆服务器
//一直监听客户请求，一旦有客户请求，立即开启新线程 
public class LoginSever {
	public static void main(String[] args) {
		// 创建一个Socket
		ServerSocket server = null;
		Socket socket = null;
		try {
			server = new ServerSocket(5000);
			while (true) {
				// 使用accept();方法来侦听并接收到此server的连接
				socket = server.accept();
				// 创建和客户端响应的线程
				LoginThread lt = new LoginThread(socket);
				// 启动线程
				lt.start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放资源
			if (socket != null)
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (server != null)
				try {
					server.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
}
